import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import Users from '@/components/user/Users.vue'
import Rights from '@/components/power/Rights'
import Roles from '@/components/power/Roles.vue'
import CateGories from '@/components/goods/CateGories.vue'
import Params from '@/components/goods/Params.vue'
import Goods from '@/components/goods/Goods.vue'
import Add from '@/components/goods/Add.vue'
import Orders from '@/components/order/Orders.vue'
import Report from '@/components/report/Report.vue'
import Layout from '@/components/report/Layout.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome,
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
      },
      {
        path: '/cateGories',
        name: 'CateGories',
        component: CateGories,
      },
      {
        path: '/params',
        name: 'Params',
        component: Params,
      },
      {
        path: '/goods',
        name: 'Goods',
        component: Goods,
      },
      {
        path: '/goods/add',
        name: 'Add',
        component: Add,
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders,
      },
      {
        path: '/reports',
        name: 'Report',
        component: Report,
      },
      {
        path: '/reportss',
        name: 'Layout',
        component: Layout,
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个页面跳转而来
  // next 是一个函数， 表示方形
  //      next() 放行 next('/') 强制跳转
  // 首页直接放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // token不为空，放行
  if (!tokenStr) return next('/login')
  next()
})
export default router
